<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">

    <link rel="stylesheet" href="/layui/css/layui.css">
    <link  href="/hg-layui-admin-ui/css/font.css">
    <script src="/js/jquery-1.8.3.min.js"></script>
    <script src="/layui/layui.js"></script>
    <script src="/ajaxfileupload.js"></script>

    <style>

        .layui-table-cell{
            display:table-cell;
            vertical-align: middle;
            height:auto;
            overflow:visible;
            text-overflow:inherit;
            white-space:normal;
        }
    </style>
</head>

<body class="layui-body-content">
<div class="layui-fluid">
    <form class="layui-form" id="cxform" action="" style="margin: 1.5% 0% 0% 0%;">
        <div class="layui-colla-item" style="margin-left: 2%">
            <div class="layui-inline">
                <label class="layui-form-label" ><span >教师姓名</span></label>
                <div  class="layui-input-inline" style="width: 170px">
                    <input type="text" id="jsxm" name="jsxm" class="layui-input" placeholder="教师姓名" >
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="margin-left: -30px"><span >课程名称</span></label>
                <div  class="layui-input-inline" style="width: 170px">
                    <input type="text" id="kcmc" name="kcmc" class="layui-input" placeholder="课程名称" >
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" ><span >学生学号</span></label>
                <div  class="layui-input-inline" style="width: 170px">
                    <input type="text" id="xsxh" name="xsxh" class="layui-input" placeholder="学生学号" >
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" ><span >学生姓名</span></label>
                <div  class="layui-input-inline" style="width: 170px">
                    <input type="text" id="xsxm" name="xsxm" class="layui-input" placeholder="学生姓名" >
                </div>
            </div>
            <div class="layui-input-inline" style="margin-left:50px;margin-top: 5px;width: 300px;">
                <button class="layui-btn layui-btn-normal layui-bg-green layui-btn-sm" id="cxbtn" type="button" lay-submit="" lay-filter="cxbtn">查询</button>
                <button class="layui-btn layui-btn-normal layui-hide layui-btn-sm" id="xzbtn" type="button" lay-submit="" lay-filter="xzbtn">新增</button>
                <button class="layui-btn layui-btn-normal layui-btn-sm "  style="background: #5FB878" id="xzmb"   type="button" lay-submit="xzmb" lay-filter="xzmb">下载模板</button>
                <input id="excelFile" name="excelFile" type="file" onchange="saveFile(this);" style="display:none"  />
                <button  id="drbtn" style="margin-left:2%;background: #5FB878"    onClick="document.getElementById('excelFile').click()" class="layui-btn layui-btn-normal layui-btn-sm "  type="button" >导 入</button>
            </div>
        </div>

        <table style="border: 1px solid red" class="layui-hide" id="test" lay-filter="test"></table>

    </form>

    <script type="text/html" id="bar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <div id="addyhdiv" style="display: none;border-radius: 6px">
        <form class="layui-form" id="formqrmain" action="" style="margin-top: 60px;margin-left:4%;">

            <input type="hidden" id="id"  name="id">
            <input type="hidden" id="ktlx"  name="ktlx">
            <!-- 新增  -->
            <div class="layui-form-item" >
                <label class="layui-form-label" style="width: 80px;"><span  >课程名称</span><span style="color: red">*</span></label>
                <div class="layui-input-inline">
                    <input  type="hidden" id="kcid"  name="kcid" />
                    <input  type="hidden" id="ktfz"  name="ktfz" />
                    <input type="text" id="kcmcs" name="kcmcs"  readonly style="width: 270px;" placeholder="课程名称" class="layui-input qyzzlname" >
                </div>

                <label class="layui-form-label" style="width: 170px"><span >课程成绩</span><span style="color: red">*</span></label>
                <div class="layui-input-inline">
                    <input type="text" id="kccj"   onkeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"  name="kccj" style="width: 270px;"  placeholder="课程成绩"  class="layui-input" />
                </div>
            </div>

            <div class="layui-form-item"  style="margin-top: 25px;">
                <label class="layui-form-label" style="width: 80px;"><span  >学分</span><span style="color: red">*</span></label>
                <div class="layui-input-inline">
                    <input type="text" id="xf" name="xf"  value="0" readonly style="width: 270px;" placeholder="学分" class="layui-input qyzzlname" >
                </div>

                <label class="layui-form-label" style="width: 170px"><span id="qyzzlmksjorfr">教师编号</span><span style="color: red">*</span></label>
                <div class="layui-input-inline">
                    <input  type="hidden" id="jsid"  name="jsid" />
                    <input type="text" id="jsbh"   name="jsbh" readonly style="width: 270px;"  placeholder="教师编号"  class="layui-input" />
                </div>
            </div>


            <div class="layui-form-item" style="margin-top: 25px;">

                <label class="layui-form-label" style="width: 80px;"><span id="qyzzlsfzhorgddh">教师姓名</span><span style="color: red">*</span></label>
                <div class="layui-input-inline">
                    <input type="text" id="jsxms" name="jsxms" readonly style="width: 270px;"  placeholder="教师姓名" class="layui-input" >
                    <!-- lay-verify="required"  -->
                </div>

                <div class=""  id="qyzzllxdz" style="margin-top: 25px;">
                    <label class="layui-form-label" style="width: 170px;">学生学号<span style="color: red">*</span></label>
                    <div class="layui-input-inline" style="width: 270px;">
                        <select id="xsxhs"  name="xsxhs"  lay-search=""   lay-filter="xsxhs">
                            <option value=" ">请选择学生学号</option>
                        </select>
                    </div>
                </div>
            </div>


            <div class="layui-form-item" style="margin-top: 25px;">
                <label class="layui-form-label" style="width: 80px;">学生姓名</label>
                <div class="layui-input-inline">
                    <input type="hidden" id="newxsxh"  name="newxsxh" >
                    <input type="text" id="xsxms"  name="xsxms" readonly placeholder="学生姓名" style="width: 270px;"  class="layui-input"  >
                </div>
            </div>

        </form>
    </div>
    <script type="text/html" id="toolbarDemo" >

        <div class="layui-btn-container" id="plscdiv" >
            <button  type="button" id="plsc1" class="layui-btn layui-btn-sm  layui-bg-red" lay-event="getCheckData">批量删除</button>
        </div>

    </script>

<!--    <div class="layui-btn-container" id="toolbarDemo" >
        <button  type="button" id="plsc" class="layui-btn layui-btn-sm  layui-hide" lay-event="getCheckData">批量删除1</button>
    </div>-->
</div>
</body>
<script>



    $(function() {
        getAllInfo();
        pdbtn();
    });

    function pdbtn(){
        $.ajax({
            type:"POST",
            url:"/admin/user/getUserInfo",
            dataType:"json",
            async : true, //是否异步  id
            success:function (data) {
                if(data.roleId!=4){
                    $("#xzbtn").hide();
                    $("#xzmb").hide();
                    $("#drbtn").hide();
                    $("#bar").html("");
                    $("#toolbarDemo").html("");
                }else{
                    var jsid = data.id;
                   // jsid = "a90f146f8c9911eabf64408d5c949bc3";
                    //jsid = "e27d24508d5c11ea8279408d5c949bc3";
                    $("#xzbtn").removeClass("layui-hide");
                    $("#plsc").removeClass("layui-hide");
                    getTeacher(jsid);
                    $("#jsid").val(jsid);
                }
            },
            error: function (data,type, err) {
                console.log("ajax错误类型："+type);
                console.log(err);
                layer.msg('删除失败！');
            }
        });
    }

    //判断新增删除按钮
    function cshsj(){
        $.ajax({
            type:"POST",
           // url:"getUserInfo"
            url:"/admin/teacher/getAllTeachers",
            data:{number:"",name:"李飞",kc:"",zc:""},
            dataType:"json",
            async : true, //是否异步
            success:function (data) {
                //根据教师id去获取教师信息
               // if(data.roleId==1){
                if(true){
                    var jsid = data.rows[0].id;
                    $("#xzbtn").removeClass("layui-hide");
                    $("#plsc").removeClass("layui-hide");
                    getTeacher(jsid);
                    $("#jsid").val(jsid);
                }
            },
            error: function (data,type, err) {
                console.log("ajax错误类型："+type);
                console.log(err);
                layer.msg('删除失败！');
            }
        });
    }

    function getTeacher(id){
        $.ajax({
            type:"POST",
            url:"/admin/teacher/getTeacher",
            data:{id:id},
            dataType:"json",
            async : true, //是否异步
            success:function (data) {
                if(data!=null){
                    $("#kcmcs").val(data.ktmc);
                    $("#kcid").val(data.kc);
                    $("#ktfz").val(data.ktfz);
                    $("#jsbh").val(data.number);
                    $("#jsxms").val(data.name);
                    $("#ktlx").val(data.ktlx);
                }
            },
            error: function (data,type, err) {
                console.log("ajax错误类型："+type);
                console.log(err);
                layer.msg('删除失败！');
            }
        });

    }


    function getAllStudents(){
        $.ajax({
            type:"POST",
            url:"/admin/student/getAllStudents",
            data:{number:"",name:"",sex:"",rxnj:""},
            dataType:"json",
            async : false, //是否异步
            success:function (data) {
                var str = "<option value=''>请选择学生学号</option> ";
                if(data.rows.length>0){
                    var datas = data.rows;
                    console.log(datas.length)
                    for (let i = 0; i < datas.length ; i++) {
                        str += "<option value="+datas[i].id+">"+datas[i].number+"</option>";
                    }
                }else{
                    str = "<option value=''>暂无学生信息</option> ";
                }
                $("#xsxhs").html(str);
                layui.use('form',function(){
                    var form = layui.form;
                    form.render();
                });
            },
            error: function (data,type, err) {
                console.log("ajax错误类型："+type);
                console.log(err);
                layer.msg('删除失败！');
            }
        });

    };

    $("#cxbtn").click(function(){
        getAllInfo();
    });

    //导入excel aaa----------------------------------------
    function saveFile(obj){

        var files = obj.files;
        for (i = 0; i < files.length; i++) {
            file = files[i]
            console.log(file);
        }
        $.ajaxFileUpload({
            url : "exportDkcjs",
            secureuri : false,//安全协议
            type : 'post',
            dataType : 'json',
            fileElementId : "excelFile",
            async : false,//同步请求
            success : function(obj) {
                console.log("返回值");
                console.log(obj);
                if(obj.code == '0000'){
                    layer.msg("导入成功!");
                    setTimeout(function(){
                        location.reload();
                    },1000);
                }else{
                    layer.msg(obj.msg);
                    setTimeout(function(){
                        location.reload();
                    },1500);
                }


            }
        });
    }

    function getAllInfo(){
        var kcmc =  $("#kcmc").val();
        var jsxm =  $("#jsxm").val();
        var xsxh =  $("#xsxh").val();
        var xsxm =  $("#xsxm").val();
        $.ajax({
            type: "POST",
            url: "getAllDkcj",
            data:{kcmc:kcmc,jsxm:jsxm,xsxh:xsxh,xsxm:xsxm},
            dataType: "json",
            success: function (data) {
                layui.use('table', function(){
                    var table = layui.table;
                    table.render({
                        elem: '#test',
                        data:data.rows,
                        height:522,
                        toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                        ,defaultToolbar: ['', '', '', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                            title: '提示'
                            ,layEvent: 'LAYTABLE_TIPS'
                            ,icon: 'layui-icon-tips'
                        }],
                        cols: [
                            [
                                {type:'checkbox'}
                                ,{field:'id', width:180, title: 'ID', hide:true ,sort: true}
                                ,{field:'xsid', width:180, title: 'ID1', hide:true ,sort: true}
                                ,{field:'ktmc', width:200, title: '课程名称', sort: true}
                                ,{field:'kccj', width:200, title: '课程成绩', sort: true}
                                ,{field:'xf', width:100, title: '学分', sort: true}
                                ,{field:'jsbh', width:200, title: '教师编号', sort: true}
                                ,{field:'jsmc', width:200,title: '教师姓名', sort: true}
                                ,{field:'xsbh', width:200,title: '学生学号', sort: true}
                                ,{field:'xsmc', width:200,title: '学生姓名', sort: true}
                                ,{fixed: 'right', width:280, align:'center', toolbar: '#bar'}
                            ]
                        ]
                        ,page: true
                    });

                });


            },
            error: function (data, type, err) {
                console.log("ajax错误类型：" + type);
                console.log(err);
                layer.msg('删除失败！');
            }
        });
    }




    $("#kccj").on("input",function(e){
        //获取input输入的值
        console.log(e.delegateTarget.value);
        var kccj = e.delegateTarget.value;
        if(kccj>60){
            var ktfz = $("#ktfz").val();
            $("#xf").val(ktfz);
        }else{
            $("#xf").val("0");
        }
    });

    layui.use(['table','form','laydate'], function() {
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var laydate = layui.laydate;

        //下载模板
        form.on('submit(xzmb)',function(){
            parent.layer.confirm('确定下载模板',{
                title:"提示信息"
            }, function(index) {
                $.ajaxFileUpload({
                    url : "exportDkcj",
                    secureuri : false,//安全协议
                    type : 'post',
                    dataType : 'json',
                    fileElementId : "excelFile",
                    async : false,//同步请求
                    success : function(obj) {
                    }
                });
                parent.layer.close(index);
            });

        });

        form.on('select(xsxhs)',function(data){
            var id = data.value;
            $.ajax({
                type:"POST",
                url:"/admin/student/getStudent",
                dataType:"json",
                data:{id:id},
                async : true, //是否异步
                success:function (data) {
                    if(data!=''&&data!=null){
                        $("#xsxms").val(data.name);
                        $("#newxsxh").val(data.number);
                    }else{
                        $("#xsxms").val("");
                    }

                },
                error: function (data,type, err) {
                    console.log("ajax错误类型："+type);
                    console.log(err);
                    layer.msg('删除失败！');
                }
            });

        });


        //执行一个laydate实例
        laydate.render({
            elem: '#fbrq' //指定元素
        });
        /*新增学生*/
        form.on('submit(xzbtn)',function () {

            var addlayer = layer.open({
                type : 1,
                title : "新增学生成绩",
                btn:['确定','取消'],
               // area : [ '52%', '70%' ],
                area : [ '865px', '500px' ],
                offset:'10%',
                shadeClose : false, //点击遮罩关闭
                content : $('#addyhdiv'),
                success : function() {
                    $("#kccj").val("");
                    $("#xf").val("");
                    $("#xsxms").val("");
                    getAllStudents();
                    layui.form.render();
                    return;
                },
                yes:function(index,layero){
                    var kccj = $("#kccj").val();
                    var jsid = $("#jsid").val();
                    var kcid = $("#kcid").val();
                    var xsid = $("#xsxhs").val();
                    var ktlx = $("#ktlx").val();
                    var xf = $("#xf").val();
                    var kcmc = $("#kcmcs").val();

                    if(kccj == null || kccj == ""){
                        layer.msg("课程成绩不能为空！");
                        return;
                    }
                    if(xsid == null || xsid == ""){
                        layer.msg("请选择学生！");
                        return;
                    }
                    var dkcj ={
                        kcmc:kcmc,
                        kccj : kccj,
                        xf : xf,
                        ktlx:ktlx,
                        kcid : kcid,
                        jsid : jsid,
                        xsid : xsid
                    };
                    console.log(dkcj);
                    $.ajax({
                        type:"POST",
                        url:"add",
                        data:dkcj,
                        success:function (data) {
                            layer.msg("增加成功");
                            layer.close(addlayer);
                            getAllInfo();
                        },
                        error: function (data,type, err) {
                            console.log("ajax错误类型："+type);
                            console.log(err);

                            layer.alert("增加失败", {
                                    icon: 6
                                },
                                function () {
                                });
                        }
                    });

                },
                cancel : function() {
                    return;
                },
                end : function() {
                    //$('#main').css("display", "none");
                }
            });


        })


        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;
            if(data.length==0){
                layer.msg("请选择数据！");
                return false;
            }
            layer.confirm('是否批量删除',{
                title:"提示信息"
            }, function(index) {
                var str ="";

                for (let i = 0; i <data.length ; i++) {
                    if(i==data.length-1){
                        str += " '"+data[i].id+"' ";
                    }else{
                        str += " '"+data[i].id+"', ";
                    }
                }
                console.log(str);
                $.ajax({
                    type:"POST",
                    url:"deletes",
                    data:{"ids":str},
                    dataType:"json",
                    success:function (data) {
                        if(data === true){//删除成功：移除删除行
                            layer.msg('提示：删除成功！');
                            getAllInfo();
                        }else{//删除失败
                            layer.msg('提示：删除失败！');
                        };
                    },
                    error: function (data,type, err) {
                        console.log("ajax错误类型："+type);
                        console.log(err);
                        hg.msg('删除失败！');
                    }
                });

                layer.close(index);
            });

            return false;
        });



        table.on('tool(test)', function(obj){
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event;
            getAllStudents();
            if(layEvent === 'edit'){

                var addlayer = layer.open({
                    type : 1,
                    title : "编辑方案信息",
                    btn:['确定','取消'],
                    area : [ '865px', '500px' ],
                    offset:'10%',
                    shadeClose : false, //点击遮罩关闭
                    content : $('#addyhdiv'),
                    success : function() {
                        console.log("jiazaighou ");
                        $("#kcmcs").val(data.ktmc);
                        $("#kccj").val(data.kccj);
                        $("#xf").val(data.xf);
                        $("#jsbh").val(data.jsbh);
                        $("#jsxms").val(data.jsmc);
                        $("#xsxhs").val(data.xsid);
                        $("#xsxms").val(data.xsmc);
                        $("#id").val(data.id);
                        form.render();
                        return;
                    },
                    yes:function(index,layero){
                        var id = $("#id").val();
                        var kccj = $("#kccj").val();
                        var jsid = $("#jsid").val();
                        var kcid = $("#kcid").val();
                        var xsid = $("#xsxhs").val();
                        var xf = $("#xf").val();
                        var kcmc = $("#kcmcs").val();

                        if(kccj == null || kccj == ""){
                            layer.msg("课程成绩不能为空！");
                            return;
                        }
                        if(xsid == null || xsid == ""){
                            layer.msg("请选择学生！");
                            return;
                        }
                        var dkcj ={
                            id:id,
                            kcmc:kcmc,
                            kccj : kccj,
                            xf : xf,
                            kcid : kcid,
                            jsid : jsid,
                            xsid : xsid
                        };
                        console.log(dkcj);
                        $.ajax({
                            type:"POST",
                            url:"edit",
                            data:dkcj,
                            success:function (data) {
                                layer.msg("修改成功");
                                layer.close(addlayer);
                                getAllInfo();
                            },
                            error: function (data,type, err) {
                                console.log("ajax错误类型："+type);
                                console.log(err);

                                layer.alert("修改失败", {
                                        icon: 6
                                    },
                                    function () {
                                    });
                            }
                        });

                    },
                    cancel : function() {
                        return;
                    },
                    end : function() {
                        //$('#main').css("display", "none");
                    }
                });
            }else if(layEvent === 'del'){
                layer.confirm('是否删除',{
                    title:"提示信息"
                }, function(index) {
                    $.ajax({
                        type:"POST",
                        url:"delete",
                        data:{"id":data.id,"xsid":data.xsid},
                        dataType:"json",
                        success:function (data) {
                            if(data === true){//删除成功：移除删除行
                                layer.msg('提示：删除成功！');
                                getAllInfo();
                            }else{//删除失败
                                layer.msg('提示：删除失败！');
                            };
                        },
                        error: function (data,type, err) {
                            console.log("ajax错误类型："+type);
                            console.log(err);
                            hg.msg('删除失败！');
                        }
                    });

                    layer.close(index);
                });

            }



        })

    })




</script>

</html>